/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.stock.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.common.collect.Lists;
import com.thinkgem.jeesite.modules.stock.entity.SpdDetail;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.stock.entity.StockWarehouse;
import com.thinkgem.jeesite.modules.stock.service.StockWarehouseService;

import java.util.List;

/**
 * 仓库库存Controller
 * @author 仓库库存
 * @version 2019-12-29
 */
@Controller
@RequestMapping(value = "${adminPath}/stock/stockWarehouse")
public class StockWarehouseController extends BaseController {

	@Autowired
	private StockWarehouseService stockWarehouseService;
	
	@ModelAttribute
	public StockWarehouse get(@RequestParam(required=false) String id) {
		StockWarehouse entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = stockWarehouseService.get(id);
		}
		if (entity == null){
			entity = new StockWarehouse();
		}
		return entity;
	}
	
	@RequiresPermissions("stock:stockWarehouse:view")
	@RequestMapping(value = {"list", ""})
	public String list(StockWarehouse stockWarehouse, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<StockWarehouse> page = stockWarehouseService.findPage(new Page<StockWarehouse>(request, response), stockWarehouse); 
		model.addAttribute("page", page);
		return "modules/stock/stockWarehouseList";
	}

	@RequiresPermissions("stock:stockWarehouse:view")
	@RequestMapping(value = {"alllist"})
	@ResponseBody
	public List<SpdDetail> alllist(StockWarehouse stockWarehouse, HttpServletRequest request, HttpServletResponse response, Model model) {
		    List<StockWarehouse> ls=stockWarehouseService.findList(stockWarehouse);
		List<SpdDetail> spdDetailList = Lists.newArrayList();
		for (StockWarehouse s:ls
			 ) {
			SpdDetail sd=new SpdDetail();
			sd.setWoods(s.getWid());
			sd.setNumber(s.getNumber());
			spdDetailList.add(sd);

		}
		return spdDetailList;

	}
	@RequiresPermissions("stock:stockWarehouse:view")
	@RequestMapping(value = "form")
	public String form(StockWarehouse stockWarehouse, Model model) {
		model.addAttribute("stockWarehouse", stockWarehouse);
		return "modules/stock/stockWarehouseForm";
	}

	@RequiresPermissions("stock:stockWarehouse:edit")
	@RequestMapping(value = "save")
	public String save(StockWarehouse stockWarehouse, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, stockWarehouse)){
			return form(stockWarehouse, model);
		}
		stockWarehouseService.save(stockWarehouse);
		addMessage(redirectAttributes, "保存仓库库存成功");
		return "redirect:"+Global.getAdminPath()+"/stock/stockWarehouse/?repage";
	}
	
	@RequiresPermissions("stock:stockWarehouse:edit")
	@RequestMapping(value = "delete")
	public String delete(StockWarehouse stockWarehouse, RedirectAttributes redirectAttributes) {
		stockWarehouseService.delete(stockWarehouse);
		addMessage(redirectAttributes, "删除仓库库存成功");
		return "redirect:"+Global.getAdminPath()+"/stock/stockWarehouse/?repage";
	}

}